package pme;

public class PoorMansExcel {
	private Entry[] table;

	/**
	 * creates a new PoorMansExcel with an array of entries which are all null
	 * 
	 * @param size
	 *            the length of the array
	 */
	public PoorMansExcel(int size) {
		table = new Entry[size];
	}

	/**
	 * creates a new PoorMansExcel with an given array of entries
	 * 
	 * @param entryArray
	 *            the entries used for PoorMansExcel
	 */
	public PoorMansExcel(Entry[] entryArray) {
		table = new Entry[entryArray.length];

		for (int i = 0; i < table.length; i++)
			table[i] = entryArray[i];
	}

	/**
	 * returns the size of the array of entries in a PoorMansExcel
	 * 
	 * @return table size
	 */
	public int getSize() {
		return table.length;
	}

	/**
	 * changes the size of the array without changing (remaining) entries
	 * 
	 * @param newsize
	 *            the new size of the array of entries
	 */
	public void resize(int newsize) {
		Entry[] oldtab = table;
		if (newsize > 0) {
			table = new Entry[newsize];
			for (int i = 0; i < newsize && i < oldtab.length; i++) {
				table[i] = oldtab[i];
			}
		}

	}

	/**
	 * writes an entry into a given line of PoorMansExcel
	 * 
	 * @param here
	 *            the line that should contain the new entry
	 * @param newEntry
	 *            the new entry for that line
	 * @return true if it worked, else false
	 */
	public boolean update(int here, Entry newEntry) {
		if (here > table.length || here < 0)
			return false;
		else {
			table[here - 1] = newEntry;
			return (table[here - 1] == null) ? true
					: table[here - 1].getDescription() == newEntry.getDescription()
							&& table[here - 1].getValue() == newEntry.getValue();
		}

	}

	/**
	 * converts a number to a string so that numbers from 1 to 999
	 * are aligned to right with width 3
	 * 
	 * @param i
	 *            the number
	 * @return a string which has a length of at least 3 chars
	 */
	private String lineToString(int i) {
		if (i <= 9)
			return "  " + i;
		else if (i <= 99)
			return " " + i;
		else
			return "" + i;
	}

	/**
	 * adds white space or cuts a string so it has a length of 60 characters
	 * 
	 * @param description
	 *            the string containing the description of an entry
	 * @return a string exactly 60 characters long
	 */
	private StringBuilder descrToString(String description) {
		int descLen = description.length();
		StringBuilder sb1 = new StringBuilder(60);

		if (descLen > 60)
			return sb1.append(description.substring(0, 60));
		else {
			sb1.append(description);
			for (int k = 1; k <= 60 - descLen; k++) {
				sb1.append(" ");
			}
			return sb1;
		}

	}

	/**
	 * creates a string out of an array of entries according to the rules
	 * 
	 * @return string containing row number, description and value.
	 */
	public String toString() {
		StringBuilder sb = new StringBuilder(100);

		for (int i = 0; i < table.length; i++) {
			sb.append(lineToString(i + 1)).append(" ")
					.append(descrToString(table[i].getDescription())).append(" ")
					.append(table[i].getValue().evaluate()).append('\n');
		}
		
		return sb.toString();
	}

}
